package com.iceengine.j3d.objects;

/**
 * The WorldObject class is a class to manage multiple geometric figures.
 * @author penguin
 */
import java.util.ArrayList;
import org.lwjgl.util.vector.*;
import org.lwjgl.opengl.GL11;

public class SimpleObject implements IObject {
    private ArrayList<IFigure> Figures;
    private Vector3f Rotation;
    private Vector3f translation;
    private Vector4f blendColor;
    private boolean blending;

    /**
     * Initializes the object.
     * @param Translation Sets the current translation of the object.
     */
    public SimpleObject(Vector3f Translation)
    {
        Figures = new ArrayList<IFigure>();
        Rotation = new Vector3f();
        translation = Translation;
        blending = false;
    }

    public void drawObject(Vector3f theTranslation, Vector3f rotation)
    {
       GL11.glLoadIdentity();
       if(blending)
           activateBlending();

       Vector3f tempTranslation = new Vector3f();

       tempTranslation.x = theTranslation.x+translation.x;
       tempTranslation.y = theTranslation.y+translation.y;
       tempTranslation.z = theTranslation.z+translation.z;

       Vector3f tempRotation = new Vector3f();

       tempRotation.x = Rotation.x + rotation.x;
       tempRotation.y = Rotation.y + rotation.y;
       tempRotation.z = Rotation.z + rotation.z;

       GL11.glRotatef(tempRotation.x, 1.0f, 0.0f, 0.0f);
       GL11.glRotatef(tempRotation.y, 0.0f, 1.0f, 0.0f);
       GL11.glRotatef(tempRotation.z, 0.0f, 0.0f, 1.0f);

       GL11.glTranslatef(tempTranslation.x, tempTranslation.y, tempTranslation.z);

        for(int i = 0; i < Figures.size(); i++) {
            Figures.get(i).DrawObject();
        }

       if(blending)
           deactivateBlending();
       
    }

    /**
     * Generates the current object.
     */
    private void genObject() {
        /*for(int i = 0; i < Figures.size(); i++) {
            Figures.get(i).setRotation(Rotation);
        }*/
    }

    /**
     * Adds a figure that implements IFigure to the ArrayList.
     * @param toAdd Figure to add to the ArrayList.
     */
    public void addFigure(IFigure toAdd)
    {
        Figures.add(toAdd);
        genObject();
    }

    public void rotateX(float angle)
    {
        Rotation.x = angle;
        genObject();
    }
    public void rotateY(float angle)
    {
        Rotation.y = angle;
        genObject();
    }
    public void rotateZ(float angle)
    {
        Rotation.z = angle;
        genObject();
    }

    // Blending

    /**
     * Sets the opengl values to activate blending.
     */
    private void activateBlending()
    {
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glColor4f(blendColor.x,blendColor.y,blendColor.z,blendColor.w);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE);
    }

    /**
     * Sets to opengl values to deactivate blending.
     */
    private void deactivateBlending()
    {
        GL11.glDisable(GL11.GL_BLEND);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
    }

    /**
     * Enable blending.
     */
    public void enableBlending()
    {
        blending = true;
    }

    /**
     * Disable blending.
     */
    public void disableBlending()
    {
        blending = false;
    }

    /**
     * Set blending values.
     * @param BlendColor Color to blend the object.
     */
    public void setBlending(Vector4f BlendColor)
    {
        blendColor = BlendColor;
    }
}
